package xyz.summer.enchatpoints;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;

import xyz.summer.enchatpoints.pojo.PlayerDate;
import xyz.summer.enchatpoints.util.ConfigurationUtil;

public class Setting {

	public final static String VERSION = "0.1.0";

	Setting() {
		PlayerDates = new HashMap<String, PlayerDate>();
		YamlConfiguration config = ConfigurationUtil
				.LoadYamlConfiguration("config.yml");
		costlevel = config.getBoolean("CostLevel");
		needpoints = config.getInt("NeedPoints");
		guipoints = config.getInt("GuiPoints");
		setpoints = config.getInt("SetPoints");
		removepoints = config.getInt("RemovePoints");
	}

	private static boolean costlevel;

	private static Map<String, PlayerDate> PlayerDates;

	public static int getGuipoints() {
		return guipoints;
	}

	public static void setGuipoints(int guipoints) {
		Setting.guipoints = guipoints;
	}

	public static int getRemovepoints() {
		return removepoints;
	}

	public static void setRemovepoints(int removepoints) {
		Setting.removepoints = removepoints;
	}

	public static int getSetpoints() {
		return setpoints;
	}

	public static void setSetpoints(int setpoints) {
		Setting.setpoints = setpoints;
	}

	private static int guipoints;

	private static int removepoints;

	private static int setpoints;

	private static int needpoints;

	public static int getNeedpoints() {
		return needpoints;
	}

	public static void setNeedpoints(int needpoints) {
		Setting.needpoints = needpoints;
	}

	static YamlConfiguration date;

	public static boolean ExistPlayerDates(String name) {
		date = ConfigurationUtil.LoadYamlConfiguration("date.yml");
		return date.isConfigurationSection(name);
	}

	public static void updatePlayerDates(String name, PlayerDate pd) {
		date = ConfigurationUtil.LoadYamlConfiguration("date.yml");
		date.set(name + ".points", pd.getPoints());
		if (pd.getLoc() != null) {
			date.set(name + ".Location.world", pd.getLoc().getWorld().getName());
			date.set(name + ".Location.X", pd.getLoc().getBlockX());
			date.set(name + ".Location.Y", pd.getLoc().getBlockY());
			date.set(name + ".Location.Z", pd.getLoc().getBlockZ());
		}else{
			if(date.isConfigurationSection(name+".Location")){
				date.set(name + ".Location.world", null);
				date.set(name + ".Location.X", null);
				date.set(name + ".Location.Y", null);
				date.set(name + ".Location.Z", null);
				date.set(name + ".Location", null);
			}
		}
		ConfigurationUtil.SaveYamlConfiguration("date.yml", date);
		PlayerDates.remove(name);
		PlayerDates.put(name, pd);
	}

	public static PlayerDate getPlayerDate(String name) {
		PlayerDate pd = PlayerDates.get(name);
		date = ConfigurationUtil.LoadYamlConfiguration("date.yml");
		if (pd == null) {
			pd = new PlayerDate();
			if (ExistPlayerDates(name)) {
				pd.setPoints(date.getInt(name + ".points"));
				if (date.isConfigurationSection(name + ".Location")) {
					Location loc = new Location(Bukkit.getWorld(date
							.getString(name + ".Location.world")),
							date.getDouble(name + ".Location.X"),
							date.getDouble(name + ".Location.Y"),
							date.getDouble(name + ".Location.Z"));
					pd.setLoc(loc);
				} else {
					pd.setLoc(null);
				}
			} else {
				pd.setPoints(0);
				pd.setLoc(null);
			}
		}
		updatePlayerDates(name, pd);
		return pd;
	}

	public static Map<String, PlayerDate> getPlayerDates() {
		return PlayerDates;
	}

	public static void setPlayerDates(Map<String, PlayerDate> playerDates) {
		PlayerDates = playerDates;
	}

	public static boolean isCostlevel() {
		return costlevel;
	}

	public static void setCostlevel(boolean costlevel) {
		Setting.costlevel = costlevel;
	}

}
